/**
 * 
 */
package net.fancy.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.fancy.main.FancyClock;
import net.fancy.main.R;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * @author fancylou
 *
 */
public class ListenerAdapter extends BaseAdapter {

	private LayoutInflater mInflater;  
    private List<HashMap<String, Object>> list;  
    private int layoutID;  
    private String flag[];  
    private int ItemIDs[];  
    
    public ListenerAdapter(Context context, List<HashMap<String, Object>> list,  
            int layoutID, String flag[], int ItemIDs[]) {
		// TODO Auto-generated constructor stub
    	this.mInflater = LayoutInflater.from(context);  
        this.list = list;  
        this.layoutID = layoutID;  
        this.flag = flag;  
        this.ItemIDs = ItemIDs; 
	}
    
	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		convertView = mInflater.inflate(layoutID, null);  
        for (int i = 0; i < flag.length; i++) {//备注1  
            if (convertView.findViewById(ItemIDs[i]) instanceof ImageView) {  
                ImageView iv = (ImageView) convertView.findViewById(ItemIDs[i]);  
                iv.setBackgroundResource((Integer) list.get(position).get(  
                        flag[i]));  
            } else if (convertView.findViewById(ItemIDs[i]) instanceof TextView) {  
                TextView tv = (TextView) convertView.findViewById(ItemIDs[i]);  
                tv.setText((String) list.get(position).get(flag[i]));  
            }else{  
                //...备注2  
            }  
        }
        addListener(convertView,position);  
		return convertView;
	}
	
	 public void addListener(View convertView,int position) {  
		 final int lo = position;
		 ((CheckBox)convertView.findViewById(R.id.ItemCheck)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				int k = lo;
				new AlertDialog.Builder(FancyClock.fc).setTitle("自定义通用SimpleAdapter").setMessage("按钮成功触发监听事件！"+k).setNegativeButton("取消", null).show(); 
			}
		});
	 }

}
